﻿#!/bin/bash
##
## Palindromik.sh
## Revision: 1.30 a.k.a. "pla1n t3xT unik0rn";
## R. J. Cano, 2012 Dec 12th
##
## This program is free software released under the
## GNU GPL 3.0 License, and it goes without
## any warranty whatsoever.
##
## Purpose: Assumes two valid filenames as
## $1 and $2 then searches for the content
## of the file $2 inside $1 based on MD5
## cheksum comparisons (A hard disk intensive task).
##
## Requeriments: To have installed a Posix BC
## interpreter, GNU file utils and the md5 utils.
##
## (Today a minimal Linux installation should
## be enough to satisfy these requeriments)
## 
## Warning: The file referred by $2 must end in
## a empty line in order to work properly.
## 

if [ $# == 2 ]; then

export k=-1;
export h1=`wc -l $1 | cut -d' ' -f1`;
export h2=`wc -l $2 | cut -d' ' -f1`;
export w=`md5sum $2 | cut -d' ' -f1`;
export e=`echo "0"`;
export c=0;
export Q=0;
export m=0;
echo " ";
echo "Processing: $2 versus $1; Please wait...";
echo " ";
while [ "$m" -lt "$h1" ]; do
export m=`echo "$k+$h2" | bc -q`;
head -n $m $1 > tmp0A;
tail -n $h2 tmp0A > tmp0B;
export e=`md5sum tmp0B | cut -d' ' -f1`;
if [ $e == `echo $w` ]; then
  export Q=`echo "$Q+1" | bc -q`;
  echo "Perfect match at line `echo "$k+1" | bc -q` (partial count of repetitions of: $Q).";
  export k=`echo "$k+$h2" | bc -q`;
else
  export k=`echo "$k+1" | bc -q`;
fi
done
echo " ";
echo "The data in $2 was found repeated: $Q time(s) inside $1.";
echo " ";
fi